using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.IO.IsolatedStorage;
namespace MicroMsg.Scene.Video
{
	public class DownloadVideoContext : IContextBase
	{
		public class ProgressInfo
		{
			public int totalLength;
			public int gotLength;
			public int msgSvrId;
			public int status;
			public void printInfo()
			{
				Log.smethod_5("DownloadVideoContext", string.Concat(new object[]
				{
					" progress: totalLen = ",
					this.totalLength,
					",gotLen =",
					this.gotLength,
					", status = ",
					(MsgUIStatus)this.status,
					", msgid = ",
					this.msgSvrId
				}));
			}
		}
		private const string TAG = "DownloadVideoContext";
		public const int STATUS_INIT = 0;
		public const int STATUS_READY = 1;
		public const int STATUS_LOADING = 2;
		public const int STATUS_ERROR = 4;
		public const int STATUS_COMPLETE = 5;
		public int mStatus;
		public IsolatedStorageFileStream mSaveFileStream;
		public ChatMsg mChatMsg;
		public MsgTrans mVideoInfo;
		public NetSceneDownloadVideo mScene;
		public DownloadVideoContext.ProgressInfo mProgressInfo = new DownloadVideoContext.ProgressInfo();
		public void updateProgressInfo(int partLen)
		{
			this.mProgressInfo.totalLength = this.mVideoInfo.nTotalDataLen;
			this.mProgressInfo.gotLength = this.mVideoInfo.nTransDataLen + partLen;
			this.mProgressInfo.msgSvrId = this.mVideoInfo.nMsgSvrID;
			this.mProgressInfo.status = DownloadVideoContext.convertStatus(this.mStatus);
			this.mProgressInfo.printInfo();
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADVIDEO_PROGRESS, this.mProgressInfo, null);
		}
		public bool needToHandle()
		{
			return this.mStatus == 0;
		}
		public bool isRunning()
		{
			if (this.mStatus != 1)
			{
				if (this.mStatus != 2)
				{
					return false;
				}
			}
			return true;
		}
		public bool needToClean()
		{
			return this.mStatus == 5 || this.mStatus == 4;
		}
		public bool isDownloadCompleted()
		{
			return this.mVideoInfo.nTransDataLen == this.mVideoInfo.nTotalDataLen && this.mVideoInfo.nTotalDataLen != 0;
		}
		public void appendVideoData(byte[] byte_0, uint startPos, int ndataLen)
		{
			if (byte_0 == null || ndataLen <= 0)
			{
				return;
			}
			this.initFileStream();
			if (this.mSaveFileStream == null)
			{
				return;
			}
			try
			{
				this.mSaveFileStream.Seek((long)((ulong)startPos), 0);
				this.mSaveFileStream.Write(byte_0, 0, ndataLen);
			}
			catch (Exception ex)
			{
				Log.smethod_1("DownloadVideoContext", ex.get_Message());
			}
		}
		private void initFileStream()
		{
			if (this.mSaveFileStream != null)
			{
				return;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists(StorageIO.getVideoPath()))
					{
						userStoreForApplication.CreateDirectory(StorageIO.getVideoPath());
					}
					if (this.mVideoInfo.strImagePath != null && userStoreForApplication.FileExists(this.mVideoInfo.strImagePath) && this.mVideoInfo.nTotalDataLen > 0 && this.mVideoInfo.nTransDataLen < this.mVideoInfo.nTotalDataLen)
					{
						Log.smethod_5("DownloadVideoContext", "The download file Exists = " + this.mVideoInfo.strImagePath);
						IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile(this.mVideoInfo.strImagePath, 3, 3);
						if (isolatedStorageFileStream.get_Length() == (long)this.mVideoInfo.nTransDataLen)
						{
							this.mSaveFileStream = isolatedStorageFileStream;
							return;
						}
						isolatedStorageFileStream.Close();
					}
					string hashString = MD5Core.GetHashString(this.mVideoInfo.strThumbnail + Util.getNowMilliseconds());
					string text = StorageIO.getVideoPath() + "/downvideo" + hashString + ".mp4";
					if (userStoreForApplication.FileExists(text))
					{
						Log.smethod_5("DownloadVideoContext", "file  exists, delete file = " + text);
						userStoreForApplication.DeleteFile(text);
					}
					Log.smethod_5("DownloadVideoContext", "create new download file  = " + text);
					IsolatedStorageFileStream isolatedStorageFileStream2 = userStoreForApplication.CreateFile(text);
					if (this.mVideoInfo != null)
					{
						this.mVideoInfo.strImagePath = text;
						this.mVideoInfo.nTransDataLen = 0;
					}
					this.mSaveFileStream = isolatedStorageFileStream2;
					StorageMgr.msgVideo.update(this.mVideoInfo);
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("DownloadVideoContext", ex.get_Message());
			}
		}
		public void onFinished()
		{
			if (this.mSaveFileStream != null)
			{
				Log.smethod_5("DownloadVideoContext", "close video file = " + this.mVideoInfo.strImagePath);
				this.mSaveFileStream.Close();
				this.mSaveFileStream = null;
			}
			this.updateChatMsg();
		}
		public void updateChatMsg()
		{
			if (this.mChatMsg != null)
			{
				this.mChatMsg.strPath = this.mVideoInfo.strImagePath;
				this.mChatMsg.nStatus = DownloadVideoContext.convertStatus(this.mStatus);
				StorageMgr.chatMsg.modifyMsg(this.mChatMsg);
				Log.smethod_5("DownloadVideoContext", "update video chatmsg ,status = " + (MsgUIStatus)this.mChatMsg.nStatus);
			}
		}
		public bool updateContext()
		{
			if (this.mVideoInfo == null)
			{
				return false;
			}
			if (this.mStatus == 5)
			{
				Log.smethod_5("DownloadVideoContext", "update download video context , delete msgtrans, msgid = " + this.mVideoInfo.nMsgSvrID);
				StorageMgr.msgVideo.delByTransID(this.mVideoInfo.nMsgTransID);
			}
			else
			{
				Log.smethod_5("DownloadVideoContext", "update download video context now, status=" + this.mStatus);
				this.mVideoInfo.nStatus = this.mStatus;
				this.mVideoInfo.nLastModifyTime = (uint)(Util.getNowMilliseconds() / 1000.0);
			}
			return true;
		}
		public static int convertStatus(int status)
		{
			MsgUIStatus result = MsgUIStatus.Processing;
			if (status == 5)
			{
				result = MsgUIStatus.Success;
			}
			else
			{
				if (status == 4)
				{
					result = MsgUIStatus.Fail;
				}
			}
			return (int)result;
		}
	}
}
